import Vue from 'vue'
import VueRouter from 'vue-router'
import HomeView from '../views/HomeView.vue'

Vue.use(VueRouter)


// // 如果有报错，加入如下
// const originalPush = VueRouter.prototype.push
// VueRouter.prototype.push = function push (location) {
//   return originalPush.call(this, location).catch((err) => err)
// }

const routes = [
  {
    path: '/',
    component: HomeView,
    redirect:"/admin/index",
    children : [
      {
        path:'/admin/index',
        component:()=>import('../views/admin/AdminIndexView.vue')
      },
      {
        path:'/admin/user',
        component:()=>import('../views/admin/AdminUserView.vue')
      },
      {
        path:'/admin/video',
        component:()=>import('../views/admin/AdminVideoView.vue')
      },
      {
        path:'/admin/videos',
        component:()=>import('../views/admin/AdminPostVideoView.vue')
      },
      {
        path:'/admin/operation',
        component:()=>import('../views/admin/AdminOperationPageView.vue')
      },
      {
        path:'/admin/log',
        component:()=>import('../views/admin/AdminLoginLogView.vue')
      },
      {
        path:'/admin/order',
        component:()=>import('../views/admin/AdminMerchandiseOrderView.vue')
      },
      {
        path:'/admin/tag',
        component:()=>import('../views/admin/AdminTagView.vue')
      },
      {
        path:'/admin/type',
        component:()=>import('../views/admin/AdminTypeView.vue')
      },
      {
        path:'/admin/rule/type',
        component:()=>import('../views/admin/AdminContentRuleTypeView.vue')
      },
      {
        path:'/admin/unaudited/comment/complaint',
        component:()=>import('../views/admin/AdminUnauditedCommentComplaintView.vue')
      },
      {
        path:'/admin/audited/comment/complaint',
        component:()=>import('../views/admin/AdminAuditedCommentComplaintView.vue')
      },
      {
        path:'/admin/unaudited/note/complaint',
        component:()=>import('../views/admin/AdminUnauditedVideoComplaintView.vue')
      },
      {
        path:'/admin/audited/video/complaint',
        component:()=>import('../views/admin/AdminAuditedVideoComplaintView.vue')
      },
      {
        path:'/admin/complaint/log',
        component:()=>import('../views/admin/AdminComplainyLogView.vue')
      },
    ]
  },
  {
    path: '/login',
    component: () => import('../views/LoginView.vue')
  },
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})


// // 路由钩子
// router.beforeEach((to, from, next) => {
//       const token = Cookies.get('token')
//       // console.log(token)
//       if (token) {
//         if (to.path === '/login') {
//           if (from.path === '/login') {
//             next('/')
//           } else {
//             next(from.path)
//           }
//         } else {
//           if (!store.state.routerList.length) {
//             loadMenus(to, next)
//           } else {
//             next()
//           }
//         }
//       } else {
//         Cookies.remove('token')
//         localStorage.removeItem('user')
//         store.commit('Set_RouterList', [])
//         // window.location.reload() 这种方法时刷新页面，了解一下~
//         if (to.path !== '/login') {
//           next('/login')
//         } else {
//           next()
//         }
//       }
//     })
//
// // 合并路由方法
// const loadMenus = (to, next) => {
//   const menus = JSON.parse(localStorage.getItem('menus'))
//   console.log(menus)
//   if (!menus) {
//     Cookies.remove('token')
//     localStorage.removeItem('user')
//     // 退出
//     return next('/login')
//   }
//   // 解析后的路由
//   const newRouter = syncRouter(menus)
//   // 存储路由
//   store.commit('Set_RouterList', newRouter)
//   // 把解析后的路由合并到routes router.addRoute() 方法
//   router.matcher = new VueRouter({}).matcher
//   // 创建一个404页面
//   const not404 = {
//     path: '*',
//     name: 'NotFound',
//     component: NotFound
//   }
//   // 把所有的路由组合成一个数组
//   const routerAll = [...newRouter, ...routes, not404]
//   routerAll.forEach(route => {
//     // 把数组中的每一项添加到导出的router上
//     router.addRoute(route)
//   })
//   next({ ...to })
// }

export default router
